//
//  ExcitableBuilderAppDelegate.m
//  ExcitableBuilder
//
//  Created by Dario on 10/06/10.
//  Copyright 2010 Dario Andrei. All rights reserved.
//

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_CHAR_PER_ROW 20


#import "ExcitableBuilderAppDelegate.h"

@implementation ExcitableBuilderAppDelegate

@synthesize window, input_choice, input_rows, input_columns, input_mode, input_starting_row, input_ending_row, input_starting_column, input_ending_column;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {

}

- (IBAction) createSpec: (id)sender {
	unsigned int i, j;
	FILE *filebk;
	
	filebk = fopen("tessuto.bk", "w");
	
	if ( filebk == NULL) {
		printf("Impossibile aprire/creare il file .bk!");
		exit(1);
	} else {
		if ([[[self.input_choice selectedCell] title] isEqual:@"Simple Mode"]) {
			
			if([[[self.input_mode selectedCell] title] isEqual:@"Square Mode"] ) { //Square Mode
				fprintf(filebk, "bio\n\nlocalities:\nlogical self;\nlogical next_1;\nlogical next_2;\nlogical next_3;\nlogical next_4;\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "physical cell_%d_%d;\n", i, j);
					}
				}
				fprintf(filebk, "\nenvironments:\n");
				
				char* n1 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n2 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n3 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n4 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						sprintf(n1, "next_1 -> cell_%d_%d", (i-1), j);
						sprintf(n2, "next_2 -> cell_%d_%d", i, (j+1));
						sprintf(n3, "next_3 -> cell_%d_%d", (i+1), j);
						sprintf(n4, "next_4 -> cell_%d_%d", i, (j-1));
						
						if(i == 0 && j == 0) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s];\n", i, j, i, j, n2, n3);
						} else if(i == 0 && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s];\n", i, j, i, j, n3, n4);
						} else if(i == 0) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s, %s];\n", i, j, i, j, n2, n3, n4);
						} else if(i == ([self.input_rows intValue] - 1) && j == 0) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s];\n", i, j, i, j, n1, n2);
						} else if(i == ([self.input_rows intValue] - 1) && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s];\n", i, j, i, j, n1, n4);
						} else if(i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n2, n4);
						} else if(j == 0) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n2, n3);
						} else if(j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n3, n4);
						} else {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, %s, %s, %s, %s];\n", i, j, i, j, n1, n3, n2, n4);
						}
					}
				}
				
				free(n1);
				free(n2);
				free(n3);
				free(n4);
				
				n1 = NULL;
				n2 = NULL;
				n3 = NULL;
				n4 = NULL;
				
				fprintf(filebk, "\nmessages: Ddp, Signal, Stimulated, Excited, Ignore;\n\nagents:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if(i == 0 && j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_2 | [Signal]@next_3 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == 0 && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_3 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_2 | [Signal]@next_3 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue] - 1) && j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_2 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue]- 1) && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_3 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_3 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_3 | [Signal]@next_4 | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n", 
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						}
					}
				}
				
				fprintf(filebk, "\nsystem:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1) &&  j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "\tcell_%d_%d::[Ddp]{60} || cell_%d_%d::CellAgent_%d_%d[X]{1} \n", i, j, i, j, i, j);
							continue;
						}
						fprintf(filebk, "\tcell_%d_%d::[Ddp]{60} || cell_%d_%d::CellAgent_%d_%d[X]{1} || \n", i, j, i, j, i, j);
					}
				}
				
				for (i = [self.input_starting_row intValue]; i <= [self.input_ending_row intValue]; ++i) {
					for (j = [self.input_starting_column intValue]; j <= [self.input_ending_column intValue]; ++j) {
						fprintf(filebk, "\t|| cell_%d_%d::[Signal]{1}\n", i, j);
					}
				}
				fprintf(filebk, "end");
			} 
			else if ([[[self.input_mode selectedCell] title] isEqual:@"List Mode"] ) { //List Mode
				//Crea una lista di cellule
				fprintf(filebk, "bio\n\nlocalities:\nlogical self;\nlogical next;\n");
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "physical cell_%d_%d;\n", i, j);
					}
				}
				fprintf(filebk, "\nenvironments:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d];\n", i, j, i, j);
							continue;
						}
						fprintf(filebk, "cell_%d_%d: [self -> cell_%d_%d, next -> cell_%d_%d];\n", i, j, i, j, i+1, j);
					}
				}
				
				fprintf(filebk, "\nmessages: Ddp, Signal, Stimulated, Excited, Ignore;\n\nagents:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
							continue;
						}
						fprintf(filebk, 
								"let CellAgent_%d_%d be \n\tX -> in( Signal )@self:1.0.[Stimulated] | [Ignore] | X1 \n\tX1 -> {[Ignore]@cell_%d_%d => 1.0} in( Signal )@self:100000.0.X1\n\t\t+ {[Ddp]@cell_%d_%d < 160.0 && [Stimulated]@cell_%d_%d => 1.0} read( Ddp )@self:0.12.[Ddp] | X1\n\t\t+ {[Ddp]@cell_%d_%d => 160.0 && [Stimulated]@cell_%d_%d => 1.0} in( Stimulated )@self:1.0.[Excited] | [Signal]@next | X1\n\t\t+ {[Ddp]@cell_%d_%d => 40.0 && [Stimulated]@cell_%d_%d <= 0.0 && [Excited]@cell_%d_%d => 1.0} in( Ddp )@self:0.12.X1\n\t\t+ {[Ddp]@cell_%d_%d < 40.0 && [Stimulated]@cell_%d_%d <= 0.0} out( Ddp )@self:1.0.X2\n\tX2 -> {[Ddp]@cell_%d_%d <= 60.0} out( Ddp )@self:1.0.[Ddp] | X2\n\t\t+ {[Ddp]@cell_%d_%d => 60.0} in( Ddp | Excited | Ignore )@self:1.0.X\nend\n",
								i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
					}
				}
				
				fprintf(filebk, "\nsystem:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if ((i == ([self.input_rows intValue] - 1)) && (j == ([self.input_columns intValue] - 1))) {
							fprintf(filebk, "\tcell_%d_%d::[Ddp]{60} || cell_%d_%d::CellAgent_%d_%d[X]{1} \n", i, j, i, j, i, j);
							continue;
						}
						
						fprintf(filebk, "\tcell_%d_%d::[Ddp]{60} || cell_%d_%d::CellAgent_%d_%d[X]{1} || \n", i, j, i, j, i, j);				
					}
				}
				
				for (i = [self.input_starting_row intValue]; i <= [self.input_ending_row intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "\t|| cell_%d_%d::[Signal]{1}\n", i, j);
					}
				}
				fprintf(filebk, "end");
			}
		}
		else /*Ion Mode*/{ 
			
			if([[[self.input_mode selectedCell] title] isEqual:@"Square Mode"] ) { //Square Mode
				fprintf(filebk, "bio\n\nlocalities:\nlogical self;\nlogical next_1;\nlogical next_2;\nlogical next_3;\nlogical next_4;\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "physical inside_%d_%d;\n", i, j);
						fprintf(filebk, "physical outside_%d_%d;\n", i, j);
					}
				}
				fprintf(filebk, "\nenvironments:\n");
				
				char* n1 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n2 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n3 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				char* n4 = malloc(sizeof(char) * MAX_CHAR_PER_ROW);
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						sprintf(n1, "next_1 -> inside_%d_%d", (i-1), j);
						sprintf(n2, "next_2 -> inside_%d_%d", i, (j+1));
						sprintf(n3, "next_3 -> inside_%d_%d", (i+1), j);
						sprintf(n4, "next_4 -> inside_%d_%d", i, (j-1));
						
						if(i == 0 && j == 0) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s];\n", i, j, i, j, n2, n3);
						} else if(i == 0 && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s];\n", i, j, i, j, n3, n4);
						} else if(i == 0) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s, %s];\n", i, j, i, j, n2, n3, n4);
						} else if(i == ([self.input_rows intValue] - 1) && j == 0) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s];\n", i, j, i, j, n1, n2);
						} else if(i == ([self.input_rows intValue] - 1) && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s];\n", i, j, i, j, n1, n4);
						} else if(i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n2, n4);
						} else if(j == 0) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n2, n3);
						} else if(j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s, %s];\n", i, j, i, j, n1, n3, n4);
						} else {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, %s, %s, %s, %s];\n", i, j, i, j, n1, n3, n2, n4);
						}
					}
				}
				
				free(n1);
				free(n2);
				free(n3);
				free(n4);
				
				n1 = NULL;
				n2 = NULL;
				n3 = NULL;
				n4 = NULL;
				
				fprintf(filebk, "\nmessages: Na, K, Signal, NaChan, KChan, Pump, Ddp, Ready, Ignore;\n\nagents:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if(i == 0 && j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_2 | [Signal]@next_3 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == 0 && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_3 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_2 | [Signal]@next_3 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue] - 1) && j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_2 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue]- 1) && j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(j == 0) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_3 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else if(j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_3 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						} else {
							fprintf(filebk, 
									"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next_1 | [Signal]@next_2 | [Signal]@next_3 | [Signal]@next_4 | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
									i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
						}
					}
				}
				
				fprintf(filebk, "\nsystem:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1) &&  j == ([self.input_columns intValue] - 1)) {
							fprintf(filebk, "\tinside_%d_%d::CellAgent_%d_%d[X]{1} || inside_%d_%d::[K]{117} || outside_%d_%d::[K]{4} || inside_%d_%d::[Na]{20} || outside_%d_%d::[Na]{63} \n", i, j, i, j, i, j, i, j, i, j, i, j, i, j);
							continue;
						}
						fprintf(filebk, "\tinside_%d_%d::CellAgent_%d_%d[X]{1} || inside_%d_%d::[K]{117} || outside_%d_%d::[K]{4} || inside_%d_%d::[Na]{20} || outside_%d_%d::[Na]{63} || \n", i, j, i, j, i, j, i, j, i, j, i, j, i, j);
					}
				}
				
				for (i = [self.input_starting_row intValue]; i <= [self.input_ending_row intValue]; ++i) {
					for (j = [self.input_starting_column intValue]; j <= [self.input_ending_column intValue]; ++j) {
						fprintf(filebk, "\t|| inside_%d_%d::[Signal]{1}\n", i, j);
					}
				}
				fprintf(filebk, "end");
			}
			else if ([[[self.input_mode selectedCell] title] isEqual:@"List Mode"]) {
				fprintf(filebk, "bio\n\nlocalities:\nlogical self;\nlogical next;\n");
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "physical inside_%d_%d;\nphysical outside_%d_%d;\n", i, j, i, j);
					}
				}
				fprintf(filebk, "\nenvironments:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d];\n", i, j, i, j);
							continue;
						}
						fprintf(filebk, "inside_%d_%d: [self -> inside_%d_%d, next -> inside_%d_%d];\n", i, j, i, j, i+1, j);
					}
				}
				
				fprintf(filebk, "\nmessages: Na, K, Signal, NaChan, KChan, Pump, Ddp, Ready, Ignore;\n\nagents:\n");
				
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if (i == ([self.input_rows intValue] - 1)) {
							fprintf(filebk, 
								"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | [Signal]@next | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
								i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
							continue;
						}
						fprintf(filebk, 
							"let CellAgent_%d_%d be \n\tX ->  in( Signal )@self:1.0.[NaChan] | [Ignore] | X1 \n\tX1  ->  {[Ignore]@inside_%d_%d => 1.0} in( Signal )@self:1000000000000000.0.X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) > -(96.485*30)} in( Na )@outside_%d_%d:0.65.[Na] | X1 \n\t\t+   {[NaChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)) <= -(96.485*30)} in( NaChan )@self:10.0.[KChan] | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) < (96.485*90)} in( K )@self:10.0.[K]@outside_%d_%d | X1 \n\t\t+   {[KChan]@inside_%d_%d > 0.0 && (8.314*310.15*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) => (96.485*90)} in( KChan )@self:0.7.[Pump] | X3 \n\n\tX3  ->  eval(CellAgent_%d_%d[X4])@self:100000.0.X2 \n\n\tX4  ->  {[Pump]@inside_%d_%d > 0.0 && [Ddp]@inside_%d_%d < 15.0} out( Ddp )@self:100000.0.X4 \n\t\t+   {[Pump]@inside_%d_%d <= 0.0} in( Ddp )@self:1.0.X4 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Ready]@inside_%d_%d <= 0.0 && [Ddp]@inside_%d_%d => 15.0} out( Ready )@self:10000.0.X4 \n\n\tX2  ->  {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d < 117.0} in( K )@outside_%d_%d:10000.0.[K]@self | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [Na]@outside_%d_%d < 63.0} in( Na )@self:10000.0.[Na]@outside_%d_%d | X2 \n\t\t+   {[Pump]@inside_%d_%d > 0.0 && [K]@inside_%d_%d => 117.0 && [Na]@outside_%d_%d => 63.0} in( Ignore | Pump | Ready )@self:10000.0.X \nend\n\n",
							i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j);
					}
				}
					
				fprintf(filebk, "\nsystem:\n");
					
				for (i = 0; i < [self.input_rows intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						if ((i == ([self.input_rows intValue] - 1)) && (j == ([self.input_columns intValue] - 1))) {
							fprintf(filebk, "\tinside_%d_%d::CellAgent_%d_%d[X]{1} || inside_%d_%d::[K]{117} || outside_%d_%d::[K]{4} || inside_%d_%d::[Na]{20} || outside_%d_%d::[Na]{63} \n", i, j, i, j, i, j, i, j, i, j, i, j, i, j);
							continue;
						}
						fprintf(filebk, "\tinside_%d_%d::CellAgent_%d_%d[X]{1} || inside_%d_%d::[K]{117} || outside_%d_%d::[K]{4} || inside_%d_%d::[Na]{20} || outside_%d_%d::[Na]{63} || \n", i, j, i, j, i, j, i, j, i, j, i, j, i, j);
					}
				}
					
				for (i = [self.input_starting_row intValue]; i <= [self.input_ending_row intValue]; ++i) {
					for (j = 0; j < [self.input_columns intValue]; ++j) {
						fprintf(filebk, "\t|| inside_%d_%d::[Signal]{1}\n", i, j);
					}
				}
				fprintf(filebk, "end");
			}
			
		}		
		fclose(filebk);
	}	
}

- (IBAction) createSim: (id) sender {
	unsigned int i, j;
	FILE *filesim;
	filesim = fopen("tessuto.sim", "w");
	
	if ( filesim == NULL) {
		printf("Impossibile aprire/creare il file .sim!");
		exit(1);
	} else {
		if ([[[self.input_choice selectedCell] title] isEqual:@"Simple Mode"]) {
			for (i = 0; i < [self.input_rows intValue]; ++i) {
				for (j = 0; j < [self.input_columns intValue]; ++j) {
					fprintf(filesim, "Exc_Cell%d%d: [ Excited ]@cell_%d_%d\nDdp_Cell%d%d: [ Ddp ]@cell_%d_%d\n", i, j, i, j, i, j, i, j);
				}
			}
		}
		else /*Ion Mode*/ {
			for (i = 0; i < [self.input_rows intValue]; ++i) {
				for (j = 0; j < [self.input_columns intValue]; ++j) {
					fprintf(filesim, 
						"\nDdp_Cell%d%d: (([NaChan]@inside_%d_%d)*( 105.5908433 - (((8.314*310.15)/(96.485))*ln([Na]@outside_%d_%d / [Na]@inside_%d_%d)))) + (([KChan]@inside_%d_%d)*( 46.0342038592 - (((8.314*310.15)/(96.485))*ln([K]@outside_%d_%d / [K]@inside_%d_%d)) )) + (([Pump]@inside_%d_%d) * (-90 + [Ddp]@inside_%d_%d))\n",
						i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j, i, j
							);
				}
			}
			
		}

		
		fclose(filesim);
	}
}

@end
